﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using DataLib.Common;
using DataLib.Accounts.Bus;

namespace Ewts.BaseData
{
    public partial class StoragePositionAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                //归属地
                ListItem item = new ListItem();
                item.Text = "--请选择--";
                item.Value = "";
               
                dropBelong.DataSource = new DataLib.BLL.City().GetList("flag1='01'").Tables[0];
                dropBelong.DataTextField = "cityname";
                dropBelong.DataValueField = "citycode";
                dropBelong.DataBind();
                dropBelong.Items.Insert(0, item);



                // 处理地
                dropgraph.DataSource = new DataLib.BLL.City().GetList("flag1='02'").Tables[0];
                dropgraph.DataTextField = "cityname";
                dropgraph.DataValueField = "citycode";
                dropgraph.DataBind();
                dropgraph.Items.Insert(0, item);


            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (this.txtstoragecode.Text.Trim().Length == 0)
            {
                strErr += "库位代码不能为空！\\n";
            }
            if (this.dropgraph.SelectedValue=="")
            {
                strErr += "请选择处理地！\\n";
            }
            if (this.dropBelong.SelectedValue == "")
            {
                strErr += "请选择归属地！\\n";
            }

            if (string.IsNullOrEmpty(this.txtgoodstype.Text))
            {
                strErr += "物品类型不能为空！\\n";
            }

            if (txtmaxvalume.Text != "")
            {
                if (!PageValidate.IsNumber(txtmaxvalume.Text))
                {
                    strErr += "最大容量必须为整数\\n";
                }
            }
            if (txtalertvalume.Text != "")
            {
                if (!PageValidate.IsNumber(txtalertvalume.Text))
                {
                    strErr += "警告容量必须为整数\\n";
                }
            }
            if (txtmaxvalume.Text != "" && txtalertvalume.Text != "")
            {
                if (decimal.Parse(txtmaxvalume.Text) < decimal.Parse(txtalertvalume.Text))
                {
                    strErr += "最大容量不能小于警告容量\\n";
                }
            }
            if (txtmaxweight.Text != "" && txtalertweight.Text != "")
            {
                if (decimal.Parse(txtmaxweight.Text) < decimal.Parse(txtalertweight.Text))
                {
                    strErr += "最大重量不能小于警告重量\\n";
                }
            }

            
            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            string storagecode = this.txtstoragecode.Text;
            string goodstype = this.txtgoodstype.Text;
            string storagetype = dropStorageType.SelectedValue;
            int maxvalume = int.Parse(this.txtmaxvalume.Text);
            int alertvalume = int.Parse(this.txtalertvalume.Text);
            decimal maxweight = decimal.Parse(this.txtmaxweight.Text);
            decimal alertweight = decimal.Parse(this.txtalertweight.Text);
            string valumeunit = this.txtvalumeunit.Text;

            DataLib.Model.StoragePostion model = new DataLib.Model.StoragePostion();
            model.storagecode = storagecode;
            model.goodstype = goodstype;
            model.storagetype = storagetype;
            model.maxvalume = maxvalume;
            model.alertvalume = alertvalume;
            model.maxweight = maxweight;
            model.alertweight = alertweight;
            model.valumeunit = valumeunit;
            model.graphicCity = dropgraph.SelectedItem.Text;
            model.belongCity = dropBelong.SelectedItem.Text;

            DataLib.BLL.StoragePostion bll = new DataLib.BLL.StoragePostion();
            bll.Add(model);
            DataLib.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "StoragePositionAdd.aspx");

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("StoragePositionList.aspx");
        }
    }
}